﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
namespace EFCoreDemo
{
    public interface BookEntityConfig : IEntityTypeConfiguration<Book>
    {
        public void Configure(EntityTypeBuilder<Book> builder)
        {
            builder.ToTable<Book>("Books").HasKey(b => b.Id); //主键
            builder.Property(b => b.ThisPerson).HasColumnName("PersonId");
            //builder.HasIndex(b => new { b.Price, b.PubTime, b.Id }); //复合索引
            //builder.Property(b => b.Title).HasMaxLength(50).IsRequired().
            //  HasColumnName("Title").HasColumnType("varchar(200)")
            //  .ValueGeneratedOnAdd() // 生成列的值
            //  .HasDefaultValue("默认值");

            //在Book类中有一个Person属性与Person类型对应
            //对应多为Person类中的Books属性 WithMany
            builder.HasOne<Person>(b => b.ThisPerson).WithMany(p => p.Books).HasForeignKey(b => b.ThisPersonId);
        }
    }
}

